<?php 

namespace controladores;

use fcf\controller as Controller,
    fcf\view as View,
	dao\fcfDao as fcfDao,
	dao\logsDao as logsDao;

/*
 * Muestra vistas con los errores 
 */
class Logger extends Controller {

	/**
	 * $error holder
	 * @var string
	 */
	private $_error = null; 
	private static $print_error = false;
	
	/**
	 * save error to $this->_error
	 * @param string $error 
	 */
	public function __construct($error){
		parent::__construct();
		$this->_error = $error;
	}

	/**
	 * load a 404 page with the error message
	 */
	public function index(){
		
		$data['title'] = '404';
		$data['error'] = $this->_error;
		View::render('error','error/404', $data);
		
	}

	/**
	 * display errors
	 * @param  array  $error an error of errors
	 * @param  string $class name of class to apply to div
	 * @return string        return the errors inside divs
	 */
	static public function display(array $error, $class = 'alert alert-danger'){
		$errorrow = null;

		if (is_array($error)){
			foreach($error as $error){
				$errorrow.= "<div class='$class'>$error</div>";
			}

			return $errorrow;
		}
	}
	
	/**
	 * in the event of an error show this message
	 */
	public static function customErrorMsg($msg) {
	
		echo "<br>".$msg."</br>";
		exit;
	}
	
	/**
	 * saved the exception and calls customer error function
	 * @param  exeption $e
	 */
	public static function exception_handler($e){
		if(ENVIRONMENT != 'production'){
			self::newMessage($e);
			self::customErrorMsg($e);
		}
	
	}
	
	/**
	 * saves error message from exception
	 * @param  numeric $number  error number
	 * @param  string $message the error
	 * @param  string $file    file originated from
	 * @param  numeric $line   line number
	 */
	public static function error_handler($number, $message, $file, $line){
		$msg = "$message in $file on line $line";
	
		if ( ($number !== E_NOTICE) && ($number < 2048) ) {
		self::errorMessage('',$msg);
		self::customErrorMsg($msg);
		}
	
		return 0;
	}
	
	/**
		* new exception
		* @param  Exception $exception
		* @param  boolean   $print_error show error or not
		* @param  boolean   $clear       clear the errorlog
		* @param  string    $error_file  file to save to
		*/
	public static function newMessage(Exception $exception, $print_error = false, $clear = false, $error_file = 'errorlog.html') {
	
		$message = $exception->getMessage();
		$code = $exception->getCode();
		$file = $exception->getFile();
		$line = $exception->getLine();
		$trace = $exception->getTraceAsString();
		$date = date('M d, Y G:iA');
	
		$log_message = "<h3>Exception information:</h3>\n
						<p><strong>Date:</strong> {$date}</p>\n
						<p><strong>Message:</strong> {$message}</p>\n
						<p><strong>Code:</strong> {$code}</p>\n
						<p><strong>File:</strong> {$file}</p>\n
						<p><strong>Line:</strong> {$line}</p>\n
						<h3>Stack trace:</h3>\n
						<pre>{$trace}</pre>\n
						<hr />\n";
				
		if( is_file($error_file) === false ) {
			file_put_contents($error_file, '');
		}
	
		if( $clear ) {
			$content = '';
		} else {
			$content = file_get_contents($error_file);
		}
	
			file_put_contents($error_file, $log_message . $content);
	
		if($print_error === true){
			echo $log_message;
			exit;
		}
	}
	
	/**
	* custom error
	* @param  string  $error       the error
	* @param  boolean $print_error display error
	* @param  string  $error_file  file to save to
	*/
	public static function errorMessage($tipo = false,$error) {
		
		$mensaje = new \stdClass();
		$error_file = 'app/tmp/errorlog.txt';
		$mensaje->tipo = $tipo;
		$mensaje->error = $error;
		$mensaje->data = date('H:i:s d-m-Y');
	
		$log_message =  json_encode($mensaje);
		if(ENVIRONMENT == 'development'){
			$info = new \stdClass();
			if(isset($_SESSION[SESSION_USUARIO]->id)){ $info->id = $_SESSION[SESSION_USUARIO]->id; }else{ $info->id = "" ;}
			$info->funcion = $tipo;
			$info->error = $error;
			logsDao::nuevo($info);
		}
		@file_put_contents($error_file,"\n".$log_message, FILE_APPEND);
	}
	
	public static function insert($funcion,$e,$query){
		
		$info = new \stdClass();
		if(isset($_SESSION[SESSION_USUARIO]->id)){ $info->id = $_SESSION[SESSION_USUARIO]->id; }else{ $info->id = "" ;}
		$info->funcion = $funcion;
		$info->error = $e->getMessage()." Query: ".$query;
		logsDao::nuevo($info);
	}
}

?>
